//package com.service.config.aspect;
//
///**
// * @ClassName: MySqlInterceptor
// * @Description:
// * @Author guochuang
// * @Date 2023/8/4
// * @Version 1.0
// */
//import com.service.config.annotation.InterceptAnnotation;
//import lombok.extern.slf4j.Slf4j;
//import net.sf.jsqlparser.statement.update.Update;
//import org.apache.ibatis.builder.annotation.ProviderSqlSource;
//import org.apache.ibatis.cache.CacheKey;
//import org.apache.ibatis.executor.Executor;
//import org.apache.ibatis.executor.statement.StatementHandler;
//import org.apache.ibatis.mapping.*;
//import org.apache.ibatis.plugin.*;
//import org.apache.ibatis.reflection.DefaultReflectorFactory;
//import org.apache.ibatis.reflection.MetaObject;
//import org.apache.ibatis.reflection.SystemMetaObject;
//import org.apache.ibatis.session.ResultHandler;
//import org.apache.ibatis.session.RowBounds;
//import org.apache.poi.ss.formula.functions.T;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
//import java.lang.annotation.Annotation;
//import java.lang.reflect.Field;
//import java.lang.reflect.Method;
//import java.sql.Connection;
//import java.util.*;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//
//@Component
//@Intercepts({
//        @Signature(
//                type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class
//        })
//})
//@Slf4j
//public class MySqlInterceptor implements Interceptor {
//    private final static Pattern DW_DIMCOMPANY = Pattern.compile("dw_dimcompany", Pattern.CASE_INSENSITIVE);
//
//    private final static String REPLACE_TXT = "(select * from dw_dimcompany where cisdel = '0' and START_PERIOD <= ? and END_PERIOD > ?)";
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//
//        if(invocation.getTarget() instanceof StatementHandler) {
//            StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
//            MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
//            MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//            String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");
//            String classPath = mappedStatement.getId();
//            if(!StringUtils.hasText(classPath)){
//                return false;
//            }
//            if (mappedStatement.getSqlCommandType() == SqlCommandType.UPDATE) {
//                ParameterMap parameterMap = mappedStatement.getParameterMap();
//                List<ParameterMapping> parameterMappings = parameterMap.getParameterMappings();
//
//                //先判断类上面是否加上了注解
//                String mapperClass = classPath.substring(0, classPath.lastIndexOf("."));
//                Class<?> classType = Class.forName(mapperClass);
//                String methodName = classPath.substring(classPath.lastIndexOf(".") + 1);
//                Method method1 = Arrays.stream(classType.getMethods()).filter(method -> method.equals(methodName)).findFirst().orElse(null);
//                assert method1 != null;
//                if(!method1.isAnnotationPresent(InterceptAnnotation.class)){
//                    return invocation.proceed();
//                }
//                InterceptAnnotation annotation = method1.getAnnotation(InterceptAnnotation.class);
//                List<String> stringList1 = Arrays.asList(annotation.oldColumn());
//
//                String newSql = originalSql;
//                Update originalSql1 = (Update) statementHandler.getBoundSql()..;
//                newSql = newSql.replaceAll("ISSYS", "ICSYS");
//                log.info("拦截后sql :" + newSql);
//                metaObject.setValue("delegate.boundSql.sql", newSql);
//            }
//
//        }
//        return invocation.proceed();
//    }
//
//
//    /**
//     * 判断是否有此注解
//     * @param classPath
//     * @return
//     * @throws ClassNotFoundException
//     */
////    private boolean hasAnnotation(String classPath) throws ClassNotFoundException {
////        if(!StringUtils.hasText(classPath)){
////            return false;
////        }
////        //先判断类上面是否加上了注解
////        String mapperClass = classPath.substring(0, classPath.lastIndexOf("."));
////        Class<?> classType = Class.forName(mapperClass);
////        if(classType.isAnnotationPresent(InterceptAnnotation.class)){
////            return true;
////        }
////        //如果类上面没有加上注解，判断方法上是否加上了注解
////        String methodName = classPath.substring(classPath.lastIndexOf(".") + 1);
////        Method method1 = Arrays.stream(classType.getMethods()).filter(method -> method.equals(methodName)).findFirst().orElse(null);
////        if(method1 == null){
////            return false;
////        }
////        return method1.isAnnotationPresent(InterceptAnnotation.class);
////    }
//
//    @Override
//    public Object plugin(Object target) {
//        if (target instanceof StatementHandler) {
//            return Plugin.wrap(target, this);
//        } else {
//            return target;
//        }
//
//    }
//
//    @Override
//    public void setProperties(Properties properties) {
//
//    }
//}
